package com.ui.zhixiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ui.zhixiao.common.R;
import com.ui.zhixiao.domain.Forum;
import com.ui.zhixiao.domain.User;
import com.ui.zhixiao.dto.ForumDto;
import com.ui.zhixiao.service.ForumService;
import com.ui.zhixiao.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/forum")
public class ForumController {
    @Autowired
    private ForumService forumService;

    @Autowired
    private UserService userService;

    /**
     * 论坛信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    private R<Page> page(int page,int pageSize,String name){
        Page<Forum> pageInfo = new Page<>(page,pageSize);
        Page<ForumDto> forumDtoPage = new Page<>();

        LambdaQueryWrapper<Forum> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(name!=null,Forum::getName,name);

        queryWrapper.orderByDesc(Forum::getPopularity);

        forumService.page(pageInfo);

        BeanUtils.copyProperties(pageInfo,forumDtoPage,"records");
        List<Forum> records = pageInfo.getRecords();
        List<ForumDto> list = records.stream().map((item)->{
            ForumDto forumDto = new ForumDto();
            BeanUtils.copyProperties(item,forumDto);
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            if(user!=null){
                String userName = user.getName();
                forumDto.setUserName(userName);
            }
            return forumDto;
        }).collect(Collectors.toList());
        forumDtoPage.setRecords(list);
        return R.success(forumDtoPage);
    }


    /**
     * 批量启用 停止
     * @param st
     * @param ids
     * @return
     */
    @PostMapping("/status/{st}")
    private R<String> setStatus(@PathVariable int st,String ids){
        String[] split = ids.split(",");
        List<Long> idList = Arrays.stream(split).map(s->Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        List<Forum> forums = idList.stream().map((item)->{
            Forum forum = new Forum();
            forum.setId(item);
            forum.setStatus(st);
            return forum;

        }).collect(Collectors.toList());

        forumService.updateBatchById(forums);
        return R.success("批量操作成功");

    }

    /**
     * 删除 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        String[] split = ids.split(",");
        List<Long> idList = Arrays.stream(split).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
        forumService.removeByIds(idList);
        return R.success("删除成功");
    }
}
